<?php

require_once "pear/XML/Unserializer.php";
require_once "pear/XML/Serializer.php";

class FileXML{
	
	var $configfile = "./user/files.xml";
	
	function _readFileList(){
		$unserializer_options = array('forceEnum' => array('folder'), 'parseAttributes'=>true );
		$unserializer = &new XML_Unserializer($unserializer_options);
		$result = $unserializer->unserialize( dirname(__FILE__) ."/" . $this->configfile, true);
		$files = $unserializer->getUnserializedData();
		return $files;		
	}
	
	function _writeFileList($wFiles){
		$serializer_options = array (
		   'addDecl' => TRUE,
		   'encoding' => 'utf-8',
		   'indent' => '  ',
		   'rootName' => 'folders',
		   'mode' => 'simplexml',
		  'defaultTagName' => 'folder',
		  'scalarAsAttributes'=> array('folderName'=>true, 'fileName'=>true, 'checkedOutBy'=>true)
		);
		$Serializer = &new XML_Serializer($serializer_options);
		$status = $Serializer->serialize($wFiles);
		$data = $Serializer->getSerializedData();
		$handle = fopen( dirname(__FILE__)."/" . $this->configfile, 'w');
		fwrite($handle, $data); 
		fclose($handle);		
		
	}
	
	////////////////////////////////////////////////////Public Methods
	
	function push_new_folder($fn){
		$files = $this->_readFileList();
		
		foreach($files['folder'] as $folder){
			if($folder['folderName'] == $fn)
				return array('valid'=>0, 'error'=>'Folder already exsists');
		}
			
		$folderObj['folderName'] = $fn;
		$folderObj['file'] = array();
		
		array_push($files['folder'], $folderObj);
		
		$this->_writeFileList($files);
		
		return array('valid'=>1, 'folderName'=>$fn);
	}
	
	function push_new_file($folder, $fileName){
		$files = $this->_readFileList();
		for($i = 0; $i < count($files['folder']); $i++){
			if($files['folder'][$i]['folderName'] == $folder){
				for($j = 0; $j < count($files['folder'][$i]['file']); $j++){
					if($files['folder'][$i]['file'][$j]['fileName'] == $fileName){
						return array('valid'=>0, 'error'=>'File already exsists');
					}
				}
				
				$fileObj['fileName'] = $fileName;
				$fileObj['checkedIn'] = 1;
				$fileObj['checkedOutBy']['name'] = "";
				
				array_push($files['folder'][$i]['file'], $fileObj);
				
				$this->_writeFileList($files);
				
				return array('valid'=>1, 'fileName'=>$fileName);					
			}else
				return array('valid'=>0, 'error'=>'Folder does not exsist');
		}
	}
	
	function getCheckedInValue($folderName, $fileName){
		$files = $this->_readFileList();
		foreach($files['folder'] as $folder){
			if($folder['folderName'] == $folderName){
				foreach($folder['file'] as $f){
					if($f['fileName'] == $fileName)
						return array('valid'=>1, 'checkedIn'=>$f['checkedIn']);
				}
			}
		}
		return array('valid'=>0, 'error'=>'File does not exsist');
	}
	
	function updateCheckOut($folderName, $fileName, $value, $name){
		$files = $this->_readFileList();
		for($i = 0; $i < count($files['folder']); $i++){
			if($files['folder'][$i]['folderName'] == $folderName){
				for($j = 0; $j < count($files['folder'][$j]['file']); $j++){
					if($files['folder'][$j]['file'][$j]['fileName'] == $fileName){
						$files['folder'][$j]['file'][$j]['checkedIn'] = $value;
						$files['folder'][$j]['file'][$j]['checkedOutBy']['name'] = $name;
						//return $files;
						$this->_writeFileList($files);
						return array('valid'=>1, 'checkInValue'=>$value, 'name'=>$name);
					}
				}
			}
		}
	}
}
?>
